/****************************************************************************************************************************************************************
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *                      _____  ___________                   ___    ______      ______           _____     _______________________                              *
 *                      __  / / /__  /_  /_____________ _    __ |  / /__(_)________  /_____________  /_    ___    |_____/_/__  __ \                             *
 *                      _  / / /__  /_  __/_  ___/  __ `/    __ | / /__  /_  __ \_  /_  _ \_  __ \  __/    __  /| |___/_/ __  / / /                             *
 *                      / /_/ / _  / / /_ _  /   / /_/ /     __ |/ / _  / / /_/ /  / /  __/  / / / /_      _  ___ |_/_/   _  /_/ /                              *
 *                      \____/  /_/  \__/ /_/    \__,_/      _____/  /_/  \____//_/  \___//_/ /_/\__/      /_/  |_/_/     /_____/                               *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *      @Project: Ultra Violent - Attack and Defend                                                                                                             *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *      @Author: Daniel "thiaZ" Koenen                                                                                                                          *
 *          @Used content                                                                                                                                       *
 *              @sscanf Plugin, foreach, Whirlpool Plugin, YSI - Y_Less                                                                                         *
 *              @zcmd - Zeex                                                                                                                                    *
 *              @Streamer Plugin, IRC Plugin - Incognito                                                                                                        *
 *              @dini - Dracoblue                                                                                                                               *
 *              @MySQL Plugin - G-sTyLeZzZ                                                                                                                      *
 *          @Special thanks to                                                                                                                                  *
 *              @Slice                                                                                                                                          *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *      @File: uvaad.pwn - Main File                                                                                                                            *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *      @This file is not released under any open source licence, you are not allowed to own this file except you have the author's permission.                 *
 *                                                                                                                                                              *
 *                                                                                                                                                              *
 *      @copyright (c) Daniel "thiaZ" Koenen 2010 - 2011 - danielkoenen@t-online.de                                                                             *
 *                                                                                                                                                              *
 ****************************************************************************************************************************************************************/          

stock CDuel::SendDuelScore() 
{
    if(duelData[iScore][0] > duelData[iScore][1]) 
    {
        SendFormattedMessage(duelData[iPlayerID][0], C_BLUE, "* You are winning! (%d - %d)", duelData[iScore] [0], duelData[iScore] [1]);
        SendFormattedMessage(duelData[iPlayerID][1], C_BLUE, "* You are losing! (%d - %d)", duelData[iScore] [0], duelData[iScore] [1]);
        return true;
    }
    
    else if(duelData[iScore] [1] > duelData[iScore] [0]) 
    {
        SendFormattedMessage(duelData[iPlayerID][1], C_BLUE, "* You are winning! (%d - %d)", duelData[iScore] [1], duelData[iScore] [0]);
        SendFormattedMessage(duelData[iPlayerID][0], C_BLUE, "* You are losing! (%d - %d)", duelData[iScore] [1], duelData[iScore] [0]);
        return true;
    }
    
    else if(duelData[iScore] [0] == duelData[iScore] [1]) 
    {
        SendFormattedMessage(duelData[iPlayerID][1], C_BLUE, "* It's a draw! (%d - %d)", duelData[iScore] [1], duelData[iScore] [0]);
        SendFormattedMessage(duelData[iPlayerID][0], C_BLUE, "* It's a draw! (%d - %d)", duelData[iScore] [1], duelData[iScore] [0]);
        return true;
    }
    
    return true;
}

stock CDuel::EndDuel(lastkiller) 
{
    CPlayer::SpawnEx(lastkiller);
    
    if(duelData[iScore][0] > duelData[iScore][1]) 
    {
        new winnerid = duelData[iPlayerID][0],
            loserid = duelData[iPlayerID][1];
            
        SendFormattedMessageToAll(C_BLUE, "* %s has beaten %s in a duel. (%d - %d | Weapons: %s & %s)", CPlayer::getName(winnerid), CPlayer::getName(loserid), duelData[iScore][0], duelData[iScore][1], getWeaponNameEx(duelData[iWeapons][0]), getWeaponNameEx(duelData[iWeapons][1]));
        playerData[winnerid][duelsWon]++;
    }
        
    if(duelData[iScore][0] < duelData[iScore][1]) 
    {
        new winnerid = duelData[iPlayerID][1],
            loserid = duelData[iPlayerID][0];
            
        SendFormattedMessageToAll(C_BLUE, "* %s has beaten %s in a duel. (%d - %d | Weapons: %s & %s)", CPlayer::getName(winnerid), CPlayer::getName(loserid), duelData[iScore][1], duelData[iScore][0], getWeaponNameEx(duelData[iWeapons][0]), getWeaponNameEx(duelData[iWeapons][1]));
        playerData[winnerid][duelsWon]++;
    }

    CDuel::FreeDuel(true);
    return true;
}

stock CDuel::DuelCount() 
{
    new sStr[20];
    duelData[iDuelCount]--;
    switch(duelData[iDuelCount]) 
    {
        case 3: sStr = "~g~ 3";
        case 2: sStr = "~y~ 2";
        case 1: sStr = "~r~ 1";
        case 0: sStr = "~g~Go!~y~Go!~r~Go!";
    }
    
    GameTextForPlayer(duelData[iPlayerID][0], sStr, 990, 4);
    GameTextForPlayer(duelData[iPlayerID][1], sStr, 990, 4);
    
    if(duelData[iDuelCount] > 0) 
    {
        CDuel::SetPlayersUpForDuel(false);
        SetTimer("alias_DuelCount", 1000, 0);
        return true;
    }
    
    else 
    {
        CDuel::SetPlayersUpForDuel(true);
        return true;
    }
}

stock CDuel::SetPlayersUpForDuel(bool:toggle) 
{
    SetPlayerPos(duelData[iPlayerID][0], duelPos[0] [d_X], duelPos[0] [d_Y], duelPos[0] [d_Z]);
    SetPlayerFacingAngle(duelData[iPlayerID][0], duelPos[0] [d_A]);
    SetPlayerHealth(duelData[iPlayerID][0], 100.0);
    SetPlayerArmour(duelData[iPlayerID][0], 100.0);
    TogglePlayerControllableEx(duelData[iPlayerID][0], toggle);
    SetCameraBehindPlayer(duelData[iPlayerID][0]);
    SetPlayerVirtualWorld(duelData[iPlayerID][0], 2);
    ResetPlayerWeapons(duelData[iPlayerID][0]);
    GivePlayerWeapon(duelData[iPlayerID][0], duelData[iWeapons][0], 9999);
    GivePlayerWeapon(duelData[iPlayerID][0], duelData[iWeapons][1], 9999);
    
    playerData[duelData[iPlayerID][0]][isDuel] = true;
    playerData[duelData[iPlayerID][1]][isDuel] = true;
    
    SetPlayerPos(duelData[iPlayerID][1], duelPos[1] [d_X], duelPos[1] [d_Y], duelPos[1] [d_Z]);
    SetPlayerFacingAngle(duelData[iPlayerID][1], duelPos[1] [d_A]);
    SetPlayerHealth(duelData[iPlayerID][1], 100.0);
    SetPlayerArmour(duelData[iPlayerID][1], 100.0);
    TogglePlayerControllableEx(duelData[iPlayerID][1], toggle);
    SetCameraBehindPlayer(duelData[iPlayerID][1]);
    SetPlayerVirtualWorld(duelData[iPlayerID][1], 2);
    ResetPlayerWeapons(duelData[iPlayerID][1]);
    GivePlayerWeapon(duelData[iPlayerID][1], duelData[iWeapons][0], 9999);
    GivePlayerWeapon(duelData[iPlayerID][1], duelData[iWeapons][1], 9999);
    return true;
}       

stock CDuel::FreeDuel(bool:Accepted) 
{
    if(Accepted == false) 
    {
        SendFormattedMessageToAll(C_BLUE, "* %s hasn't accepted the duel invitation from %s!", CPlayer::getName(duelData[iPlayerID] [1]), CPlayer::getName(duelData[iPlayerID] [0]));
    }
    
    playerData[duelData[iPlayerID][0]][isDuel] = false;
    playerData[duelData[iPlayerID][1]][isDuel] = false;
    
    duelData[iPlayerID][0] = INVALID_PLAYER_ID;
    duelData[iPlayerID][1] = INVALID_PLAYER_ID;
    duelData[iWeapons][0] = -1;
    duelData[iWeapons][1] = -1;
    duelData[iScore][0] = 0;
    duelData[iScore][1] = 0;
    duelData[bIsRunning] = false;
    return true;
}

stock CDuel::PlayerDeath(killerid, playerId) 
{
    if(playerData[killerid][isDuel] == true) 
    {
        if(killerid == duelData[iPlayerID][0]) 
        {
            duelData[iScore][0]++;
            CDuel::SendDuelScore();
            
            if((duelData[iScore][0] + duelData[iScore][1]) == 3) 
            {
                if(duelData[iScore][0] > duelData[iScore][1]) 
                {
                    playerData[playerId][isDuel] = false;
                    playerData[killerid][isDuel] = false;
                    CDuel::EndDuel(killerid);
                }
                
                else if(duelData[iScore][0] < duelData[iScore][1]) 
                {
                    playerData[playerId][isDuel] = false;
                    playerData[killerid][isDuel] = false;
                    CDuel::EndDuel(killerid);
                }
            }
        }
        
        if(killerid == duelData[iPlayerID][1]) 
        {
            duelData[iScore][1]++;
            CDuel::SendDuelScore();
            
            if((duelData[iScore][0] + duelData[iScore][1]) == 3) 
            {
                if(duelData[iScore][0] > duelData[iScore][1]) 
                {
                    playerData[playerId][isDuel] = false;
                    playerData[killerid][isDuel] = false;
                    CDuel::EndDuel(killerid);
                }
                
                else if(duelData[iScore][0] < duelData[iScore][1]) 
                {
                    playerData[playerId][isDuel] = false;
                    playerData[killerid][isDuel] = false;
                    CDuel::EndDuel(killerid);
                }
            }
        }
    }
    
    return true;
}

forward alias_FreeDuel(bool:Accepted);
public alias_FreeDuel(bool:Accepted) 
{
    CDuel::FreeDuel(Accepted);
    return true;
}

forward alias_DuelCount();
public alias_DuelCount() 
{
    CDuel::DuelCount();
    return true;
}
